﻿using markdown_picture_packer.common.constants;
using markdown_picture_packer.common.enums;
using markdown_picture_packer.common.utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace markdown_picture_packer.frontend.panels
{
    public class SelectPathPanel : BasePanel
    {
        private SelectPathType Type;
        private TextBox Tb;
        private Button Btn;
        private EventHandler SelectFileEvent;
        private EventHandler SelectDirEvent;
        public SelectPathPanel(SelectPathType type)
        {
            BindEvent(type);
        }
        public override void InitComponent()
        {
            Btn = new Button { BackgroundImage = Properties.Resources.ChooseFile, Font = Constants.MAIN_FONT, BackgroundImageLayout = ImageLayout.Stretch };
            Tb = new TextBox { Font = Constants.MAIN_FONT };
        }
        public override void InitSize(int newWdith, int newHeight)
        {
            Btn.Size = new Size(newHeight, newHeight);
            Tb.Size = new Size(FrontUtil.GetFillWidth(newWdith, Btn), newHeight);

        }
        public override void InitLocation()
        {
            Tb.Location = new Point(0, 0);
            FrontUtil.OnRight(Btn, Tb, 0);
        }
        public override void InitEvent()
        {
            base.InitEvent();
            SelectFileEvent = (s, e) =>
            {
                using (OpenFileDialog dialog = new OpenFileDialog { Title = EnumDictionary.SELECT_PATH_TYPE[Type], InitialDirectory = Constants.DESKTOP_PATH, Filter = "Markdown 文件|*.md", Multiselect = false, CheckFileExists = true, })
                    if (dialog.ShowDialog() == DialogResult.OK) Tb.Text = dialog.FileName;
            };
            SelectDirEvent = (s, e) =>
            {
                using (var dialog = new FolderBrowserDialog { Description = EnumDictionary.SELECT_PATH_TYPE[Type], SelectedPath = Constants.DESKTOP_PATH, ShowNewFolderButton = true })
                    if (dialog.ShowDialog() == DialogResult.OK) Tb.Text = dialog.SelectedPath;
            };
        }
        public override void AddComponent()
        {
            Controls.Add(Btn);
            Controls.Add(Tb);
        }
        public void BindEvent(SelectPathType type)
        {
            Type = type;
            Tb.Text = "";
            bool isFile = Type == SelectPathType.FILE;
            Btn.Click -= isFile ? SelectDirEvent : SelectFileEvent;
            Btn.Click += isFile ? SelectFileEvent : SelectDirEvent;
        }
        public string GetVal()
        {
            return Tb.Text.Trim();
        }
    }
}
